package org.ddevil.data.util;

import java.util.Collection;
import java.util.Hashtable;

/**
 * @author Rich O'Connell
 */
public class DataUtils {

	/**
	 * Utility method to check if an Object has some value that
	 * is not null and not an empty string.
	 *
	 * @param o
	 * @return
	 */
	public static final boolean hasValue(Object o){
	    if(o != null && !o.toString().equals("")){
	        return true;
	    }
	    return false;
	}

	/**
	 * An equals() method that is safe to use with <code>null</code> values.
	 * Only null == null.
	 *
	 * @param o1
	 * @param o2
	 * @return
	 */
	public static final boolean safeEquals(Object o1, Object o2) {
	    if (o1 == null) {
	        return o2 == null;
	    }
	    return o1.equals(o2);
	}

	/**
	 * Checks if the value is contained in the givena array.
	 *
	 * @param array
	 *            The array to check.
	 * @param value
	 *            The Value to check for.
	 * @return True if value is in array, else false.
	 */
	public static boolean arrayContains(Object[] array, Object value) {
	    if (DataUtils.getIndexInArray(array, value) > -1) {
	        return true;
	    }
	    return false;
	}

	/**
	 * A convenience method to get the index of the given item in an array.
	 *
	 * @param dbName
	 *            The DB Name of the column you want the index for.
	 * @return The index of the column you want.
	 */
	public static int getIndexInArray(Object[] array, Object item) {
	    for (int i = 0; i < array.length; i++) {
	        if (array[i].equals(item))
	            return i;
	    }
	    return -1;
	}

	/**
	 * This will format a double array of Strings for outputting as one big
	 * String with nice little dots in between each String pairing.
	 *
	 * @param array
	 * @return
	 */
	public static String formatDoubleArrayForPrint(String[][] array) {

	    StringBuffer sb = new StringBuffer();

	    int pad = 40;
	    int numdots = 0;
	    sb.append("\n");

	    sb.append("--------------------------------------------------\n");

	    for (int i = 0; i < array.length; i++) {

	        sb.append(array[i][0]);
	        numdots = pad - array[i][0].length();

	        for (int j = 0; j < numdots; j++)
	            sb.append(".");

	        sb.append("[" + array[i][1] + "]");

	        sb.append("\n");
	    }

	    sb.append("--------------------------------------------------\n");

	    return sb.toString();

	}

	/**
	 * This will print a double array of Strings to the console in a nice
	 * format.
	 *
	 * @param array
	 */
	public static void prettyPrintDoubleArray(String[][] array) {
	    System.out.println(formatDoubleArrayForPrint(array));
	}

	/**
	 * This will print a Hashtable to the console in a nice format. The
	 * Hashtable is converted to a double array of Strings, with each key value
	 * pairing being toString()'d.
	 *
	 * @param hash
	 */
	public static void prettyPrintHashtable(Hashtable hash) {
	    prettyPrintDoubleArray(DataUtils.hashtableToArray(hash));
	}

	/**
	 * Convert a hashtable to a two dimensional array.
	 *
	 * @param in
	 *            the hashtable to be converted
	 * @return A double String array representation of the passed in Hashtable
	 */
	public static String[][] hashtableToArray(Hashtable in) {
	    String[][] out = new String[in.size()][2];

	    int index = 0;
	    for (java.util.Enumeration e = in.keys(); e.hasMoreElements();) {
	        Object key = e.nextElement();

	        // set the key value in the array
	        out[index][0] = key.toString();

	        // set the value in the array
	        out[index][1] = in.get(key).toString();

	        index++;
	    }

	    return out;
	}

	/**
	 * Takes the first n elements of the String array and return them as a new
	 * array in the same order, where n is the newLength parameter passed in. If
	 * newLength > inArray.length you will get an IndexOutOfBoundsExeption.
	 *
	 * @param inArray
	 *            Array to truncate.
	 * @param newLength
	 *            The desired length of the new array.
	 * @return A truncated array of size newLength.
	 */
	public static String[] truncateArray(String[] inArray, int newLength) {
	    String[] ret = new String[newLength];
	    for (int i = 0; i < newLength; i++) {
	        ret[i] = inArray[i];
	    }
	    return ret;
	}

	/**
	 * Build a comma separated list of each element in the list using a toString.
	 * @param collection
	 * @return
	 */
	public static String collectionToString(Collection<?> collection) {
		if (collection.isEmpty()) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (Object e : collection) {
			sb.append(e.toString());
			sb.append(",");
		}
		return sb.substring(0, sb.lastIndexOf(","));
	}
}
